package gold.digger;

import gold.utils.InputUtil;
import gold.vo.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC1609 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean isEvenOddTree(TreeNode root) {
            int curLevel = 0;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);

            while (!queue.isEmpty()) {
                int size = queue.size(), prev = curLevel % 2 == 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE;
                for (int i = 0; i < size; i++) {
                    TreeNode cur = queue.poll();
                    if (curLevel % 2 == 0) {
                        // 当前是偶数层
                        if (cur.val % 2 == 0 || cur.val <= prev) return false;
                    } else {
                        // 当前是奇数层
                        if (cur.val % 2 != 0 || cur.val >= prev) return false;
                    }

                    prev = cur.val;
                    if (null != cur.left) queue.offer(cur.left);
                    if (null != cur.right) queue.offer(cur.right);
                }
                ++curLevel;
            }

            return true;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        TreeNode ro = TreeNode.createTree("[1,10,4,3,null,7,9,12,8,6,null,null,2]");
//        System.out.println(solution.isEvenOddTree(ro));

//        System.out.println(solution.isEvenOddTree(TreeNode.createTree("[5,4,2,3,3,7]")));
//        System.out.println(solution.isEvenOddTree(TreeNode.createTree("[5,9,1,3,5,7]")));
//        System.out.println(solution.isEvenOddTree(TreeNode.createTree("[1]")));
        System.out.println(solution.isEvenOddTree(TreeNode.createTree("[11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17]")));
    }

    public static void main(String[] args) throws Exception {
        LC1609 an = new LC1609();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
